var login = {
	isVipShow: false,
	checkPhonenumber: function () {
		var $phones = $.trim($("#user_name").val());
		var $isMobile = /(^1(3|4|5|7|8)\d{9}$)|(^[a-zA-Z]+$)/;
		if (!$phones) {
			$(".error_icon1").show();
			$(".phone_tips").show();
			$('.phone_tips_y').hide();
			$(".engPhone").hide();
			return false;
		} else if (!$isMobile.test($phones)) {
			$(".phone_tips").hide();
			$(".engPhone").hide();
			$(".error_icon1").show();
			$('.phone_tips_y').show();
			return false;
		} else {
			$(".phone_tips").hide();
			$('.phone_tips_y').hide();
			$(".error_icon1").hide();
			$(".engPhone").hide();
			return true;
			// 手机号码是否存在
		}
	},
	checkLoginName: function () {
		var userName = $.trim($("#user_name").val());
		if (!userName) {
			$(".error_icon1").show();
			$(".phone_tips").show();
			$('.phone_tips_y').hide();
			$(".engPhone").hide();
			return false;
		} else {
			$(".phone_tips").hide();
			$('.phone_tips_y').hide();
			$(".error_icon1").hide();
			$(".engPhone").hide();
			return true;
		}
	},
	checkPassword: function () {
		var $code = $.trim($("#login_password").val());
		var $isPattern = /^[a-zA-Z0-9\W]{6,20}$/;
		if (!$code) {
			$('.error_icon2').show();
			$('.code_tips_y').show();
			$('.code_tips').hide();
			return false;
		} else if (!$isPattern.test($code)) {
			$('.error_icon2').show();
			$('.code_tips').show();
			$('.code_tips_y').hide();
			return false;
		} else {
			$('.error_icon2').hide();
			$('.code_tips').hide();
			$('.code_tips_y').hide();
			return true;
		}
	},
	// 记住用户名
	rem_username: function () {
		if ($.ls("bp_userName") && $("#rem_userName:checked")) {
			$("#user_name").val($.ls("bp_phoneNum"));
		} else {
			$("#user_name").val("");
		}
	},
	// 登录
	loginSuccess: function () {
		var user_name = $("#user_name").val();
		var login_password = $("#login_password").val();
		// 记住用户名
		if (!login.checkLoginName()) {
			return;
		}
		if (!login.checkPassword()) {
			return;
		}
		$.ls("bp_token", null);
		$.ls("bp_userName", null);
		$.ls("bp_memberInfo", null);
		$.ls("bp_memberCode", null);
		var data = {
			"loginName": user_name,
			"password": login_password
		};
		$.commonAjax({
			url: "B007",
			type: "POST",
			openDialog: true,
			data: data,
			isShowError: true,
			success: function (res) {
				if (res.code && res.code == 200) {
					var token = res.data.token;
					var _name = res.data.member.loginName;
					if (res.data.member.nickName) {
						_name = res.data.member.nickName;
					}
					var phoneNum = "";
					if (res.data.member.loginName) {
						phoneNum = res.data.member.loginName;
					}
					$.ls("bp_phoneNum", phoneNum);
					$.ls("bp_token", token);
					$.ls("bp_userName", _name);
					$.ls("bp_memberInfo", res.data.member);
					$.ls("bp_memberCode", res.data.member.code);
					$.ls("bp_loginIP", res.data.member.lastLoginIp);
					$.ls("bp_vip", res.data.member.vip);
					login.rem_username();
					if (res.data.member.enterpriseAudit != undefined && res.data.member.enterpriseAudit == 3) {
						$.ls("bp_headerIcon", "enterprise");
					} else {
						$.ls("bp_headerIcon", "personal");
					}
					if ($.ls("bp_headerIcon") && $.ls("bp_headerIcon") == "enterprise") {
						$(".img_wrap img").attr("src", "../img/company.png").attr("title", "企业用户");
						$(".ordinary_grad").html($.ls('bp_Mintro'));
					} else {
						$(".img_wrap img").attr("src", "../img/default_head.png").attr("title", "个人用户");
						$(".ordinary_grad").html($.ls('bp_Mintro'));
					}
					login.redirect();
				} else {
					layer.msg(res.msg);
				}
			},
			error: function (res) {
				$.layermsg(res.msg);
			}
		})
	},
	// 登录成功跳转
	redirect: function () {
		var redirecturl = location.search;
		if ($.trim(redirecturl)) {
			redirecturl = redirecturl.substring(1, redirecturl.length);
			redirecturl = decodeURIComponent(redirecturl)
		}
		if (redirecturl) {
			window.location.href = redirecturl;
		} else {
			window.location.href = "/html/index.html";
		}
	}
}

$(function () {
	// 初始化用户名
	login.rem_username();
	// 校验登录名
	$("#user_name").on("blur", function () {
		login.checkLoginName();
	});
	// 校验密码
	$("#login_password").on("blur", function () {
		if (!login.checkLoginName()) {
			return;
		}
		login.checkPassword();
	});
	$("#loginTo").on("click", function () {
		login.loginSuccess();
	})
})